#region Using directives

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Windows.Forms;

using FighterChessEngine.Event;
using FighterChessEngine.Enum;
using FighterChessEngine.FC_Board;

#endregion

namespace FighterChessSkin.FC_Board
{
    /// <summary>
    /// Summary description for UISquare.
    /// </summary>
    public class UISquare
    {
        #region Atributes
        private readonly string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        private readonly string squareDir = "Images\\Board\\Square";
        private readonly int topReserve = 24;
        private readonly int height = 27;
        private readonly int width = 27;
        
        private Square square;
        private Point location; // top-left
        private Graphics gOff;
        private Bitmap imgBoard;
        private bool isFlashing;
        
        #endregion

        #region Properties
        public Graphics Graphic
        {
            get { return gOff; }
            set { gOff = value; }
        }
        public bool Flashing
        {
            get { return isFlashing; }
            set { isFlashing = value; }
        }
        public int Row
        {
            get
            {
                return square.Row;
            }
        }
        public int Col
        {
            get
            {
                return square.Col;
            }
        }
        #endregion

        #region Methods
        public UISquare()
        {
            isFlashing = false;
        }

        public UISquare(Square square, Graphics gOff)
            : this()
        {
            this.square = square;
            this.gOff = gOff;
            this.location = CalculateSquareCoordinate(square.Row, square.Col);
        }

        public UISquare(Square square, Graphics gOff, Bitmap imgBoard)
            : this(square, gOff)
        {
            this.imgBoard = new Bitmap((Image)imgBoard);
        }

        public void Draw()
        {
            DrawBackground();
            DrawForeground();
        }

        public void DrawForeground()
        {
            ImageAttributes att = new ImageAttributes();
            att.SetColorKey(Color.FromArgb(255, 0, 255), Color.FromArgb(255, 0, 255));
            Rectangle rect = new Rectangle(location.X, location.Y, width, height);

            Bitmap img = null;
            string strDir = appDir + "\\" + squareDir;
            switch (square.Type) /*TODO*/ // draw square img with specific type
            {
                case SquareType.HpRestoreBlack:
                    img = new Bitmap(strDir + "\\" + "hp_restore_black.png");
                    break;
                case SquareType.HpRestoreWhite:
                    img = new Bitmap(strDir + "\\" + "hp_restore_white.png");
                    break;
                case SquareType.LevelUpBlack:
                    img = new Bitmap(strDir + "\\" + "level_up_black.png");
                    break;
                case SquareType.LevelUpWhite:
                    img = new Bitmap(strDir + "\\" + "level_up_white.png");
                    break;
                case SquareType.Random:
                    img = new Bitmap(strDir + "\\" + "random_square.png");
                    break;

                default:
                    return;
            }

            gOff.DrawImage((Image)img, rect, 0, 0, width, height, GraphicsUnit.Pixel, att);
        }

        public void DrawBackground()
        {   
            Rectangle rect = new Rectangle(location.X, location.Y, width, height);
            gOff.DrawImage((Image)this.imgBoard, rect, rect, GraphicsUnit.Pixel);
        }

        public void DrawFlashing()
        {
            Bitmap imgFlashing = new Bitmap(appDir + "\\" + squareDir + "\\" + "flashing_square.png");
            ImageAttributes att = new ImageAttributes();
            att.SetColorKey(Color.FromArgb(255, 0, 255), Color.FromArgb(255, 0, 255));
            Rectangle destRect = new Rectangle(location.X, location.Y, imgFlashing.Width, imgFlashing.Height);
            gOff.DrawImage((Image)imgFlashing, destRect, 0, 0, imgFlashing.Width, imgFlashing.Height, GraphicsUnit.Pixel, att);
            //gOff.DrawImage((Image)imgFlashing, location.X, location.Y);
        }

        public void ClearFlashing()
        {
            Draw();
        }

        public bool Contains(int xCoord, int yCoord, Point boardLocation)
        {   
            return ((xCoord >= location.X && xCoord <= location.X + width + boardLocation.X)
                && yCoord >= location.Y && yCoord <= location.Y + height + boardLocation.Y);
        }
        #endregion

        #region Support Funcs
        private Point CalculateSquareCoordinate(int row, int col)
        {
            Point result = new Point();
            result.X = col * width;
            result.Y = row * height;
            result.Y += topReserve;

            return result;
        }
        #endregion
    }
}
